package cn.hanyx.coder.design.pattern.behavioral.observer.demo02;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * UserController
 *
 * @author hanyx
 * @date 2020/06/03
 */
public class UserController {

    private EventBus eventBus;

    public UserController() {

//        this.eventBus = new EventBus();  //同步阻塞模式
        /**
         * 异步非阻塞
         */
        this.eventBus = new AsyncEventBus(Executors.newFixedThreadPool(8));
    }

    public void setReqObserver(List<Object> observers) {
        observers.forEach(observer -> eventBus.register(observer));
    }

    public Integer register(String phone, String password) {
        // 省略注册逻辑
        Integer userId = 10001;

        /**
         * 只会通知参数是Integer 的观察者类
         */
        eventBus.post(userId);
        return userId;
    }

    public static void main(String[] args) {
        UserController userController = new UserController();
        // 注册观察者
        List<Object> list = new ArrayList();
        list.add(new RegNotificationObserver());
        list.add(new RegPromotionObserver());
        userController.setReqObserver(list);

        // 用户注册
        userController.register("110", "234");
    }


}
